package cn.hd01.weixin;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hd01.common.BaseConstant.RebateSummaryType;
import cn.hd01.repository.entity.Customer;
import cn.hd01.repository.entity.CustomerBankCard;
import cn.hd01.repository.entity.RebateBook;
import cn.hd01.repository.entity.RebateSummary;
import cn.hd01.repository.entity.WithdrawBook;
import cn.hd01.service.CustomerBankCardService;
import cn.hd01.service.CustomerService;
import cn.hd01.service.RebateBookService;
import cn.hd01.service.RebateSummaryService;
import cn.hd01.service.WithdrawBookService;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.auth.AuthType;
import cn.hd01.web.util.WebHelper;
import cn.hd01.weixin.model.Result;

@Controller
@RequestMapping("/weixin/rebate")
@Auth(AuthType.WECHAT)
public class WXRebateController {

	@Autowired
	private RebateSummaryService summaryService;

	@Autowired
	private WithdrawBookService wbService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private CustomerBankCardService bcService;
	
	@Autowired
	private RebateBookService rebateBookService;

	@RequestMapping(path = "", method = RequestMethod.GET)
	public String index(Model m) {
		Customer customer = customerService.findByOauthId(WebHelper.currentWXOauth().getId());
		RebateSummary summary = summaryService.findByRefIdAndType(customer.getId(), RebateSummaryType.DISTRIBUTOR);

		String allRebate = "0";
		String allWithdrawRebate = "0";
		if (summary != null) {
			allRebate = summary.getAllRebate().subtract(summary.getAllWaitWithdrawRebate()).subtract(summary.getAllWithdrawRebate()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
			allWithdrawRebate = summary.getAllWithdrawRebate().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
		}
		
		m.addAttribute("id", customer.getId());
		m.addAttribute("allRebate", allRebate);
		m.addAttribute("allWithdrawRebate", allWithdrawRebate);
		return "weixin/supplier/rebate";
	}

	@RequestMapping(path = "/cash", method = RequestMethod.GET)
	public String cash(Model m, @RequestParam(required = false) Integer cardId) {
		Customer customer = customerService.findByOauthId(WebHelper.currentWXOauth().getId());
		CustomerBankCard bankCard = null;
		if (cardId != null) {
			WebHelper.session().removeAttribute("op");
			bankCard = bcService.findOne(cardId);
		}

		if (bankCard == null) {
			bankCard = bcService.findByDefaultCard(customer.getId());
		}

		if (bankCard == null) {
			WebHelper.session().setAttribute("op", "cash");
			return "redirect:/weixin/bank";
		}

		m.addAttribute("card", bankCard);

		RebateSummary summary = summaryService.findByRefIdAndType(customer.getId(), RebateSummaryType.DISTRIBUTOR);
		String allRebate = "0";
		if (summary != null) {
			allRebate = summary.getAllRebate().subtract(summary.getAllWaitWithdrawRebate()).subtract(summary.getAllWithdrawRebate()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
		}
		m.addAttribute("allRebate", allRebate);

		return "weixin/supplier/cash";
	}

	@ResponseBody
	@RequestMapping(path = "/cash", method = RequestMethod.POST)
	public Result submitCash(WithdrawBook wb) {
		Customer customer = customerService.findByOauthId(WebHelper.currentWXOauth().getId());
		RebateSummary summary = summaryService.findByRefIdAndType(customer.getId(), RebateSummaryType.DISTRIBUTOR);
		if (summary == null || wb.getWithdrawRebate().compareTo(summary.getAllRebate()) > 0) {
			return new Result(false, "余额不足");
		}

		BigDecimal after = summary.getAllRebate().subtract(wb.getWithdrawRebate());
		wb.setRefId(summary.getRefId());
		wb.setType(summary.getType());
		wb.setAfterAllWithdrawRebate(after);
		wb.setName(customer.getName());

		summary.setAllWaitWithdrawRebate(summary.getAllWaitWithdrawRebate().add(wb.getWithdrawRebate()));

		summaryService.save(summary);
		wbService.save(wb);

		return new Result();
	}

	@RequestMapping(path = "/selectCard", method = RequestMethod.GET)
	public String cash(Model m) {
		WebHelper.session().setAttribute("op", "cash");
		return "redirect:/weixin/bank";
	}
	
	@ResponseBody
	@RequestMapping(path = "/rebateBook/list", method = RequestMethod.GET)
	public Page<RebateBook> bookList(RebateBook rebateBook, Integer distributorId, @PageableDefault Pageable pageable) {
		Sort sort = new Sort(Direction.DESC, "createTime");
		Pageable page = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
		rebateBook.setRefId(distributorId);
		return rebateBookService.findAll(rebateBook, page);
	}

}
